/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright Blazebit
 */

package com.blazebit.persistence.testsuite;

import com.blazebit.persistence.CriteriaBuilder;
import com.blazebit.persistence.testsuite.entity.*;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
 *
 * @author Christian Beikov
 * @since 1.2.0
 */
public class CollectionJoinTestHibernate extends AbstractCoreTest {

    @Override
    protected Class<?>[] getEntityClasses() {
        return new Class<?>[]{
            Root2.class,
            IndexedNode2.class,
            KeyedNode2.class,
            KeyedEmbeddable.class,
            IndexedEmbeddable.class
        };
    }

    @Test
    public void testOneToManyMappedBy() {
        CriteriaBuilder<Root2> criteria = cbf.create(em, Root2.class, "r");
        criteria.select("r.indexedNodesMappedBy[0]");
        criteria.select("r.keyedNodesMappedBy['default']");

        assertEquals("SELECT indexedNodesMappedBy_0_1, " + joinAliasValue("keyedNodesMappedBy_default_1") + " FROM Root2 r"
                + " LEFT JOIN r.indexedNodesMappedBy indexedNodesMappedBy_0_1"
                + onClause("INDEX(indexedNodesMappedBy_0_1) = 0")
                + " LEFT JOIN r.keyedNodesMappedBy keyedNodesMappedBy_default_1"
                + onClause("KEY(keyedNodesMappedBy_default_1) = 'default'"), criteria.getQueryString());
        criteria.getResultList();
    }

}
